
# -*- coding: utf-8 -*-

from django.db import models
from django.contrib.auth.models import User
from django.utils.encoding import force_unicode
from comunidadcyt.cargoAcademico.models import CargoAcademico
import unicodedata

class UserProfile(models.Model):
    user = models.ForeignKey(User,unique=True)
    telefono = models.CharField(max_length = 200,blank = True, null = True, verbose_name="Teléfono")
    interno = models.CharField(max_length = 50,blank = True, null = True, verbose_name="Interno")
    celular = models.CharField(max_length = 200, blank = True, null = True)
    sexo = models.CharField(max_length=1,blank = True, null = True)
    pais = models.CharField(max_length=30,blank = True, null = True, verbose_name="País")
    fecha_nacimiento = models.DateField(blank = True, null = True, verbose_name="Fecha de nacimiento")
    foto = models.ImageField(max_length=300,upload_to='media/Usuarios',blank=True,null=True,default="static/images/default.png")
    informacion_personal = models.TextField(blank=True,null=True, verbose_name="Información Personal")
    cargo = models.ManyToManyField(CargoAcademico,related_name='Usuarios',blank=True,null=True)
    cv = models.FileField(max_length=300,upload_to = 'media/Usuarios', blank = True, null = True)
    calendar = models.TextField(blank=True,null=True, verbose_name="Calendario")
    titulo = models.CharField(max_length=15,blank = True, null = True, verbose_name="Título", default="---")
    motivo = models.CharField(max_length=500, blank = False, null = False, verbose_name="* Referencia")

    def get_absolute_url(self):
        return "/comunidadcyt/accounts/profileSummary/%s/" % self.user.id
    
    def save(self, **args):
        for field in self._meta.fields:
            if field.name == 'foto':
                field.upload_to = 'media/Usuarios/%s' % unicodedata.normalize('NFKD', self.user.username).encode('ascii','ignore').replace(" ", "")
            if field.name == 'cv':
                field.upload_to = 'media/Usuarios/%s/cv' % unicodedata.normalize('NFKD', self.user.username).encode('ascii','ignore').replace(" ", "")
                
        super(UserProfile, self).save()
    
    class Admin:
        pass

    def __unicode__(self):
        return force_unicode(self.user.get_full_name())
